import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { GameController } from "../../../mvc/GameController";
import { LadderBattlefieldUtil } from "../../ladderBattlefield/LadderBattlefieldUtil";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { RuneItem } from "./cell/RuneItem";
import { RuneData } from "./data/RuneData";
import { ItemData } from "../../../resource/info/ItemData";
import { TItem } from "../../../../../Message/Db/Tables/TItem";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { RuneUtil } from "./RuneUtil";
import { MsgManager } from "../../../manager/MsgManager";
import { Cache } from "../../../cache/Cache";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { ERuneType } from "../../../../../Message/Public/ERuneType";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { RuneItemData } from "./data/RuneItemData";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
type int = number;
//class SkillRunePackWindow
    
    export  class SkillRunePackWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            setSize(250, 354);
            titleHeight = 26;
            title = Language.getString(44042);
            this.layer = GameController.skill.view as BaseWindow;
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            this.mouseEnabled = !LadderBattlefieldUtil.isInLadderBattlefield;
            this.mouseChildren = !LadderBattlefieldUtil.isInLadderBattlefield;
            this.updateBackpackRune();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=0;
            var loc3=0;
            var loc4=null;
            super.createChildren();
            var loc1=new GTextFormat(FontUtil.songtiName, 12, 15788607);
            loc1.indent = 7;
            ObjCreate.createSpBannerAndBg(17, 38, 216, 303, Language.getString(44043), this, loc1);
            loc2 = 0;
            while (loc2 < 6) 
            {
                loc3 = 0;
                while (loc3 < 5) 
                {
                    loc4 = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                    ObjCreate.setObjAttri(loc4, 27 + loc3 * 40, 72 + loc2 * 40, -1, -1, this);
                    ++loc3;
                }
                ++loc2;
            }
            this._canSetTileList = ObjCreate.createTileList(27, 72, 202, 240, this);
            this._canSetTileList.rowHeight = 36;
            this._canSetTileList.columnWidth = 36;
            this._canSetTileList.horizontalGap = 4;
            this._canSetTileList.verticalGap = 4;
            this._canSetTileList.setStyle("skin", new Object /* flash.display.Bitmap */());
            this._canSetTileList.setStyle("cellRenderer", RuneItem);
            this._canSetPageSelecter = ObjCreate.createPageSelecter(86, 317, this);
            this.addListeners();
            return;
        }

        /* internal  */addListeners(): void
        {
            this._canSetTileList.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.onCanSetTileDblClick);
            this._canSetPageSelecter.addEventListener(flash.events.Event.CHANGE, this.onCanSetPageChange);
            return;
        }

        /* internal  */onCanSetTileDblClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=null;
            if (typeof arg1.target === "runeitem") 
            {
                loc1 = arg1.target as RuneItem;
                loc2 = new RuneData();
                loc2.pos = this._relatedPos;
                loc2.itemData = loc1.itemData;
                if (loc1.itemData && this.isCanMoveIn(loc1.itemData)) 
                {
                    this.checkInsert(loc2);
                }
            }
            return;
        }

        /* internal  */checkInsert(arg1: RuneData): void
        {
            var runeData: RuneData;
            var sameCode: int;
            var runeIndex: int;
            var tipText: string;
            var posItemData: ItemData;
            var isShowTip: Boolean;
            var onChooseCheck: Function;
            var index: int;
            var tipTitle: string;
            var tItem: TItem;

            var loc1;
            onChooseCheck = null;
            index = 0;
            tipTitle = null;
            tItem = null;
            runeData = arg1;
            onChooseCheck = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.RuneInsert, runeData));
                }
                return;
            }
            sameCode = RuneUtil.getSameEffectItemCode(runeData.itemData);
            if (sameCode > 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(44063));
                index = Cache.instance.pack.runePackCache.getIndexByCode(sameCode);
                Dispatcher.dispatchEvent(new DataEvent(EventName.RuneSelect, index));
                return;
            }
            if (ItemsUtil.isHolyRune(runeData.itemData)) 
            {
                if (runeData.itemData.runeSwallowCodes == null || runeData.itemData.runeSwallowCodes.length == 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(80326));
                    return;
                }
            }
            runeIndex = this.getRuneCanInsertIndex(runeData.itemData);
            if (runeIndex == -1) 
            {
                tipTitle = Language.getString(44065);
                if (!(runeData.itemData == null) && !(runeData.itemData.itemInfo == null) && !(runeData.itemData.itemInfo.item == null)) 
                {
                    tItem = runeData.itemData.itemInfo.item;
                    if (tItem.category == ECategory._ECategoryRune && tItem.type == 1) 
                    {
                        tipTitle = Language.getString(44064);
                    }
                    else if (tItem.category == ECategory._ECategoryRune && tItem.type == 3) 
                    {
                        tipTitle = Language.getString(44064);
                    }
                }
                MsgManager.showRollTipsMsg(Language.getStringByParam(44066, tipTitle));
                return;
            }
            runeData.pos = runeIndex;
            Dispatcher.dispatchEvent(new DataEvent(EventName.RuneSelect, runeIndex));
            tipText = Language.getStringByParam(44051, runeData.itemData.name);
            posItemData = Cache.instance.pack.runePackCache.getItemDataByIndex(runeData.pos);
            isShowTip = false;
            if (posItemData && !(posItemData == ItemData.nullItemData)) 
            {
                tipText = tipText + Language.getStringByParam(44052, posItemData.name);
            }
            if (!ItemsUtil.isBind(runeData.itemData)) 
            {
                isShowTip = true;
                tipText = tipText + Language.getString(44053);
            }
            if (isShowTip) 
            {
                Alert.show(tipText, null, Alert.OK | Alert.CANCEL, null, onChooseCheck);
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.RuneInsert, runeData));
            }
            return;
        }

        /* internal  */getRuneCanInsertIndex(arg1: ItemData): int
        {
            var loc1=1;
            var loc2=1;
            if (ItemsUtil.isRune(arg1) && (arg1.type == ERuneType._ERuneTypeNormal || arg1.type == ERuneType._ERuneTypeNormalHoly)) 
            {
                loc1 = 1;
                loc2 = 2;
                if (Cache.instance.rune.runeInfo) 
                {
                    loc2 = Cache.instance.rune.runeInfo.normalExtraNum;
                }
            }
            else if (ItemsUtil.isRune(arg1) && (arg1.type == ERuneType._ERuneTypeSkill || arg1.type == ERuneType._ERuneTypeSkillHoly)) 
            {
                loc1 = 5;
                loc2 = 8;
                if (Cache.instance.rune.runeInfo) 
                {
                    loc2 = Cache.instance.rune.runeInfo.skillExtraNum + 4;
                }
            }
            else 
            {
                return -1;
            }
            var loc3=loc1;
            while (loc3 <= loc2) 
            {
                arg1 = Cache.instance.pack.runePackCache.getItemDataByIndex(loc3);
                if (!(arg1 && !(arg1 == ItemData.nullItemData))) 
                {
                    return loc3;
                }
                ++loc3;
            }
            return -1;
        }

        /* internal  */onCanSetPageChange(arg1: Object /* flash.events.Event */): void
        {
            this.updateCanSetList();
            return;
        }

        public updateCanSetData(arg1: Array<any>): void
        {
            this._aryCanSetData = arg1;
            this._canSetPageSelecter.maxPage = Math.ceil(this._aryCanSetData.length / this.pageSize);
            this.updateCanSetList();
            return;
        }

        /* internal  */updateCanSetList(): void
        {
            var loc1=new Array<any>();
            var loc2=(this._canSetPageSelecter.currentPage - 1) * this.pageSize;
            var loc3=Math.min(this._canSetPageSelecter.currentPage * this.pageSize, this._aryCanSetData.length);
            var loc4=loc2;
            while (loc4 < loc3) 
            {
                loc1.push(this._aryCanSetData[loc4]);
                ++loc4;
            }
            this._canSetTileList.dataProvider = new DataProvider(loc1);
            if (loc1.length == 0) 
            {
                this._canSetTileList.selectedIndex = -1;
            }
            return;
        }

        public updateBackpackRune(): void
        {
            var loc3=null;
            var loc4=null;
            var loc1=Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isRune);
            var loc2=new Array<any>();
            var loc5=0;
            while (loc5 < loc1.length) 
            {
                loc3 = loc1[loc5] as ItemData;
                loc4 = new RuneItemData(loc1[loc5] as ItemData, this.isCanMoveIn(loc3));
                loc2.push(loc4);
                ++loc5;
            }
            this.updateCanSetData(loc2);
            return;
        }

        /* internal  */isCanMoveIn(arg1: ItemData): Boolean
        {
            if (arg1.itemInfo.item.career == Cache.instance.role.entityInfo.career || arg1.itemInfo.item.career == 0) 
            {
                return true;
            }
            return false;
        }

        public set relatedPos(arg1: int)
        {
            this._relatedPos = arg1;
            this.updateBackpackRune();
            return;
        }

        /* internal */ /* const */pageSize: int=30;

        /* internal */ /* var */_canSetTileList: GTileList;

        /* internal */ /* var */_canSetPageSelecter: PageSelecter;

        /* internal */ /* var */_aryCanSetData: Array<any>;

        /* internal */ /* var */_relatedPos: int=0;
    }
